package org.abettor.pushbox.activity2;

/**
 * 解决了该地图用户列表的详情
 */
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.abettor.pushbox.config.Config;
import org.abettor.pushbox.download.DownloadMap;
import org.abettor.pushbox.download.ParaDownloadRetValue;
import org.abettor.pushbox.model.LastTimeIdInterface;
import org.abettor.pushbox.model.ResovedMapUserBean;
import org.abettor.pushbox.R;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class ResovedMapUserList extends AbstractListActivity {
	private DownloadMap netBeanDown = null;// 用来下载地图的bean
	private int map_id = -1;// 地图的id
	private final int VIEW_USER_DETAIL = Menu.FIRST + 1;//查看用户详情
	private final int SEND_USER_MSG = Menu.FIRST + 2;//给用户发送消息的操作
	/*---------------------------------------------------------*/
	//注册context 事件
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle(R.string.net_map);
		menu.add(0, VIEW_USER_DETAIL, 1, R.string.view_user_detail);
		menu.add(0, SEND_USER_MSG,2,R.string.send_msg);
	}
	@Override
	public boolean onContextItemSelected(MenuItem aItem){
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) aItem
		.getMenuInfo();
		switch (aItem.getItemId()){
		case VIEW_USER_DETAIL://查看用户详情
			ResovedMapUserBean bean = (ResovedMapUserBean)userMapAdapter.getItem(info.position);
			doViewUserDetail(bean.getId(),bean.getResovler());
			break;
		case SEND_USER_MSG:
			doSendMsg((int)info.id);
			break;
		}
		return false;
	}
	//发送消息的操作
	private void doSendMsg(int id){
		Intent in = new Intent();
		Bundle extras = new Bundle();
		extras.putInt("_id", (int) id);
		in.putExtras(extras);
		in.setClass(ResovedMapUserList.this,
				SiteMessageActivity.class);
		startActivity(in);
	}
	/*---------------------------------------------------------*/
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle(R.string.label_internet_map);
		Intent intent = getIntent();
		Bundle extras = intent.getExtras();
		map_id = extras.getInt("_id");
		netBeanDown = new DownloadMap(this);
		this.getListView().setOnItemClickListener(
				new ResovedListItemClickListenner());
		// 注册context menu的操作
		registerForContextMenu(this.getListView());
	}

	// 点击的操作
	private class ResovedListItemClickListenner implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> l, View v, int position, long id) {
			ResovedMapUserBean bean = (ResovedMapUserBean)userMapAdapter.getItem(position);
			Log.i("xyz", bean.getId()+":"+id);
			doViewUserDetail((int) bean.getId(),bean.getResovler());
		}
	}

	/**
	 * 查看用户的详情
	 * 
	 * @param id
	 */
	private void doViewUserDetail(int id,String value) {
		Intent in = new Intent();
		Bundle extras = new Bundle();
		extras.putInt("_id", (int) id);
		extras.putString("nick_name", value);
		in.putExtras(extras);
		in.setClass(ResovedMapUserList.this, ShowUserDetailActivity.class);
		startActivity(in);
	}

	@Override
	protected View layoutView(int position, View convertView, ViewGroup parent,
			LastTimeIdInterface bean) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		final ResovedMapUserBean item = (ResovedMapUserBean) bean;
		LayoutInflater inflater = ResovedMapUserList.this.getLayoutInflater();
		ViewGroup theParent = (ViewGroup) inflater.inflate(
				R.layout.resoved_map_user_list_item, parent, false);
		TextView resolvedView = (TextView) theParent
				.findViewById(R.id.resolved_msg);
		TextView timeView = (TextView) theParent
				.findViewById(R.id.resolve_time);
		resolvedView.setText(changeMsgToStr(item));
		timeView.setText(sdf.format(item.getUploadDate()));
		return theParent;
	}

	private String changeMsgToStr(ResovedMapUserBean bean) {
		String str = "";
		str = str
				+ ResovedMapUserList.this
						.getText(R.string.resoved_map_user_nick) + ":";
		str = str + bean.getResovler();
		return str;
	}

	@Override
	protected String loadContentFromNet(Date time) {
		String content = netBeanDown.downResolvedListStr(map_id, time,
				Config.PAGESIZE);
		return content;
	}

	@Override
	protected int isHasMoreData(String content) {
		return ParaDownloadRetValue.isHasMoreResovedUser(content);
	}

	@SuppressWarnings({"unchecked" })
	@Override
	protected List paraData(String content) {
		final List reList = ParaDownloadRetValue.paraResovedUser(content);
		return reList;
	}
	@Override
	protected void alertMsg()
	{
	}
	@Override
	protected boolean needDialog()
	{
		return false;
	}
}
